package com.nucarf.bi.digtalize.function;

import com.alibaba.fastjson.JSON;
import com.nucarf.bi.digtalize.bean.SinkKafkaBean;
import org.apache.flink.streaming.connectors.kafka.KafkaSerializationSchema;
import org.apache.kafka.clients.producer.ProducerRecord;

import javax.annotation.Nullable;
import java.nio.charset.StandardCharsets;

public class KafkaSinkSchema implements KafkaSerializationSchema<SinkKafkaBean> {

    public ProducerRecord<byte[], byte[]> serialize(SinkKafkaBean sinkBean, @Nullable Long aLong) {
        try {
            return new ProducerRecord(sinkBean.getTopic(), sinkBean.getCus_code().getBytes(StandardCharsets.UTF_8), JSON.toJSONString(sinkBean.getSendBean()).getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
